<!-- Used by media_seek_perf to record seek perf metrics. -->
<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>CNS Seek Tests</title>
     <script src="utils.js" type="text/javascript"></script>
  </head>

  <body>
    <video controls></video>
    <div></div>
  </body>

  <script type="text/javascript">
    var video = document.querySelector("video");
    var logDiv = document.querySelector("div");
    var ITERATIONS = 3;

    var SeekTestCase = {
      SHORT: 0,
      LONG: 1
    }

    var CachedState = {
      UNCACHED: 0,
      CACHED: 1
    }

    function log(text) {
      logDiv.innerText += text + "\n";
    }

    function resetSeekRecords() {
      seekRecords = [];
      for (cache_index in Object.keys(CachedState)) {
        seekRecords[cache_index] = [];
        for (seek_index in Object.keys(SeekTestCase)) {
           seekRecords[cache_index][seek_index] = [];
        }
      }
    }

    // Called by the PyAuto controller to initiate testing.
    function startTest(src) {
      if (window.domAutomationController)
        window.domAutomationController.send(true);

      resetSeekRecords();
      endTest = false;
      errorMsg = "";
      timer = new Timer();

      video.addEventListener("playing", playing);
      video.addEventListener("seeked",  seeked);
      video.addEventListener("error", error);
      originalSrc = src;
      log("Running tests on " + originalSrc);
      log("Starting seek tests without browser caching:");
      cacheState = CachedState.UNCACHED;
      iteration = 0;
      IterationTest();
    }

    function IterationTest() {
      if (iteration < ITERATIONS) {
        iteration++;
        log("Test iteration " + iteration);
        seekState = SeekTestCase.SHORT;
        video.src = getVideoSrc();
        video.play();
      } else if (cacheState == CachedState.UNCACHED) {
        log("Starting seek tests with browser caching:");
        cacheState = CachedState.CACHED;
        iteration = 0;
        IterationTest();
      } else {
        endTest = true;
      }
    }

    function getVideoSrc() {
      if (cacheState == CachedState.UNCACHED) {
        return GenerateUniqueURL(originalSrc);
      } else {
        return video.src;
      }
    }

    function playing() {
      if (seekState == SeekTestCase.SHORT) {
        timer.start();
        video.currentTime = 1;
      }
    }

    function seeked() {
      delta = timer.stop();
      switch (seekState) {
        case SeekTestCase.SHORT:
          seekRecords[cacheState][SeekTestCase.SHORT].push(delta);
          log ("short seek in " + delta + "ms.")
          seekState = SeekTestCase.LONG;
          timer.start();
          video.currentTime = video.duration - 1;
          break;
        // Seek to almost end of file (unbuffered area).
        case SeekTestCase.LONG:
          seekRecords[cacheState][SeekTestCase.LONG].push(delta);
          log("long seek in " + delta + "ms.")
          IterationTest();
          break;
        default:
          end("An un-expected seek occured.");
      }
    }

    function end(msg) {
      errorMsg = msg;
      endTest = true;
      log(msg);
    }

    function error(evt) {
      end("Error loading media: " + evt.target + " " + evt.type + " " +
          video.error.code);
    }
  </script>
</html>
